package org.example.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.example.pojo.Tasks;
import org.example.service.TasksService;
import org.example.utils.R;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: TasksController
 * Description:
 *
 * @Author Kotaro
 * @Create 2024/11/26 16:07
 * @Version 1.0
 */
@CrossOrigin //允许其他源访问我们的controller
@RestController
@RequestMapping("tasks")
@Slf4j
public class TasksController {

    @Resource
    private TasksService tasksService;

    @PutMapping("receiver")//这里是志愿者接受任务的端口
    public R receiverTasks(@RequestBody Tasks tasks){
        R r = null;
        if(tasks.getReceiverId() != null){
            r = tasksService.selectTasksStatusByReceiverId(tasks.getReceiverId() , tasks.getTaskId());
        } else {
            r = R.error("没有接收到接受人ID",null);
        }
        return r;
    }

    @PostMapping("requester")//这里是老人发起任务的端口
    public R requesterTasks(@RequestBody Tasks tasks){
        R r = null;
        if (tasks.getRequesterId() != null) {
            r = tasksService.selectTasksStatusByRequesterId(tasks);

        }else {
            r = R.error("没有接收到发起人ID",null);
        }
        return r;
    }

    @PutMapping("complete")//这里是志愿者完成任务的端口
    public R complete(@RequestBody Tasks tasks){
        R r = tasksService.completeTasksByTasksId(tasks.getTaskId());
        return r;
    }

    @PutMapping("review")//这里是老人评价任务的端口
    public R reviewTasks(@RequestBody Tasks tasks){
        R r = tasksService.reviewTasksByTasksId(tasks.getTaskId() , tasks.getRating() , tasks.getComment());
        return r;
    }

    @GetMapping("show")//根据任务类别|老人ID|志愿者ID|任务状态查询所有该类别任务
    public R showAllTasks(@RequestParam(required = false) String requesterId,
                          @RequestParam(required = false) String receiverId,
                          @RequestParam(required = false) Integer taskCategory,
                          @RequestParam(required = false) Integer taskStatus,
                          @RequestParam(defaultValue = "1") Integer currentPage){
        if(requesterId != null){
            if(taskStatus != null){
                R r = tasksService.pageByStringAndInteger(10,currentPage,requesterId , taskStatus);
                log.info("查询的数据为：{}" , r);
                return r;
            }else {
                R r = tasksService.pageByString(1000,1,requesterId);
                log.info("查询的数据为：{}" , r);
                return r;
            }
        } else if (receiverId != null){
            if(taskStatus != null){
                R r = tasksService.pageByStringAndInteger(10,currentPage,receiverId , taskStatus);
                log.info("查询的数据为：{}" , r);
                return r;
            }else {
                R r = tasksService.pageByString(1000,1,receiverId);
                log.info("查询的数据为：{}" , r);
                return r;
            }
        } else {
                R r = tasksService.page(10,currentPage,taskCategory);
                log.info("查询的数据为：{}" , r);
                return r;
        }
    }

    @DeleteMapping("recall/{taskId}")
    public R recallTasks(@PathVariable Integer taskId) {
        R r = tasksService.recallTasksByTaskId(taskId);
        return r;
    }
    @PostMapping("reviewTask")
    public R reviewTasks(@RequestParam Integer taskId,@RequestParam Integer requesterId ,@RequestParam Integer rating,@RequestParam String remark,@RequestParam String comment){
        R r = tasksService.reviewTasksByTasksIdOne(taskId , requesterId ,rating,remark,comment);
        return r;
    }

}
